package cn.onlineoedering.user.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.codehaus.jackson.map.ObjectMapper;

import cn.onlineoedering.user.entity.GeneralUser;
import cn.onlineoedering.user.service.UserService;
import cn.onlineoedering.user.service.impl.UserserviceImpl;

public class UserAction implements SessionAware ,ServletResponseAware,ServletRequestAware{
	private GeneralUser user;
	private UserService service = new UserserviceImpl();
	private Map<String, Object> session;
	private HttpServletResponse response;
	private HttpServletRequest request;

	public String login() {
		if (!user.getUserName().equals("") || user.getUserName() != null || user.getUserName()=="") {
			GeneralUser generalUser = service.queryByName(user.getUserName());

			if (!generalUser.getUserName().equals("") || generalUser != null)
				System.out.println("success");
			session.put("username", generalUser);
			return "success";
		}
		return "input";
	}

	public void register() {
		String userName=request.getParameter("generalUser.userName");
		System.out.println(userName);
		try {
			if (userName != "" || !userName.equals("") || userName != null) {
				if (service.queryByName(userName) == null) {
					GeneralUser generalUser = new GeneralUser(0,userName,
							request.getParameter("generalUser.password"),
							request.getParameter("generalUser.role"),
							request.getParameter("generalUser.gender"),
							request.getParameter("generalUser.phone"),
							request.getParameter("generalUser.QQ"),
							request.getParameter("generalUser.cardId"),
							request.getParameter("generalUser.address"));
					service.save(generalUser);
					ObjectMapper mapper = new ObjectMapper();
					mapper.writeValue(response.getOutputStream(),generalUser);
				} else {
					ObjectMapper mapper = new ObjectMapper();
					mapper.writeValue(response.getOutputStream(), userName+ "已经被注册！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public GeneralUser getUser() {
		return user;
	}

	public void setUser(GeneralUser user) {
		this.user = user;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response=arg0;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}
}
